package com.example.forest.job;

import com.alibaba.fastjson.JSONObject;
import com.example.forest.demos.web.TqInterfaces;
import org.apache.commons.codec.digest.DigestUtils;

import javax.annotation.Resource;

public class TqRefreshTokenJob {

    @Resource
    TqInterfaces tqInterfaces;
    public static final String ACCOUNT="江苏航天大为科技股份有限公司";
    public static final String SECRET="Aa123456789*";
    public static String ACCESS_TOKEN="";
    public static String REFRESH_TOKEN="";
    public static final int SUCCESS_CODE = 0;

    public void refreshToken(){
        if ("".equals(TqRefreshTokenJob.ACCESS_TOKEN)) {
            JSONObject response = tqInterfaces.getAccessToken(null, ACCOUNT, DigestUtils.md5Hex(SECRET), "7200");
            if (SUCCESS_CODE==response.getInteger("code")) {
                JSONObject result = response.getJSONObject("result");
                ACCESS_TOKEN = result.getString("accessToken");
                REFRESH_TOKEN = result.getString("refreshToken");
            }
        }else{
            JSONObject object = tqInterfaces.refreshToken(null, ACCESS_TOKEN, REFRESH_TOKEN, "7200");
            if (SUCCESS_CODE==object.getInteger("code")) {
                JSONObject result = object.getJSONObject("result");
                ACCESS_TOKEN = result.getString("accessToken");
                REFRESH_TOKEN = result.getString("refreshToken");
            }else{
                JSONObject response = tqInterfaces.getAccessToken(null, ACCOUNT, DigestUtils.md5Hex(SECRET), "7200");
                if (SUCCESS_CODE==response.getInteger("code")) {
                    JSONObject result = response.getJSONObject("result");
                    ACCESS_TOKEN = result.getString("accessToken");
                    REFRESH_TOKEN = result.getString("refreshToken");
                }
            }
        }
    }
}
